//
//  HomeCellPictureView.swift
//  SinaWeiBo
//
//  Created by 有风 on 16/2/22.
//  Copyright © 2016年 YouFeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HomeCellPictureView: UICollectionView {
    
    var imageURLs: [NSURL]? {
        didSet {
            let picSize = calculatePictureViewSize()
            
            //2.更新配图视图的大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(picSize)
            }
            reloadData()
            
            testLabel.text = "\(imageURLs?.count ?? 0)"

        }
    }
    let itemWidth = (ScreenWidth - StatusCellMargin * 2 - 2 * PictureCellMargin) / 3

    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        //设置layout
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        //设置最小间距
        flowLayout.minimumInteritemSpacing = PictureCellMargin
        flowLayout.minimumLineSpacing = PictureCellMargin

        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: "picCell")
        //设置数据源
        self.dataSource = self
        
        self.backgroundColor = UIColor.whiteColor()
        
        
        self.scrollsToTop = false
        //添加测试label
        self.addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    private func calculatePictureViewSize() -> CGSize {
        
        let imageCount = imageURLs?.count ?? 0
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        if  imageCount == 1 {
            let imageSize = CGSizeMake(150, 200)
            
            layout.itemSize = imageSize
            return imageSize
        }
        
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)

        if imageCount == 4 {
            //配图视图大小的计算
            let w = itemWidth * 2 + PictureCellMargin
            return CGSize(width: w, height: w)

        }
        //1.计算行数
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = itemWidth * 3 + PictureCellMargin * 2
        let h = itemWidth * row + (row - 1) * PictureCellMargin
        return CGSizeMake(w, h)
    }
    
    
    private lazy var testLabel: UILabel = UILabel(text: "", fontSize: 40, textColor: UIColor.redColor())

    

}

extension HomeCellPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("picCell", forIndexPath: indexPath) as! PictureCell
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
    }
}




private class PictureCell: UICollectionViewCell {
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }
    
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(iconView)
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }

    }

    
    
    private lazy var iconView: UIImageView = {
        let iv = UIImageView()
        //UIImageView默认的显示方式  ScaleToFill: 缩放全部视图进行填充
        //显得很山寨
        //        iv.contentMode = .ScaleToFill
        // Aspect: 部分  缩放部分 进行填充  视图的比例不会失真 但是 图片会被裁剪
        iv.contentMode = .ScaleAspectFill
        //缩放图片  部分图片进行适应的显示  会导致 imageView可能会留白  一般也不推荐使用这样方式来显示图片
        //        iv.contentMode = .ScaleAspectFit
        //手动设置裁剪
        iv.clipsToBounds = true
        return iv
    }()
}







